package top.kaoshanji.s1ch09b;

import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/30 15:40
 */
@Component
@RequestScope
public class LoginProcessor {

    private final LoggedUserManagementService loggedUserManagementService;

    private String username;
    private String password;

    // 注入会话作用域的bean
    public LoginProcessor(LoggedUserManagementService loggedUserManagementService) {
        this.loggedUserManagementService = loggedUserManagementService;
    }

    /**
     * 代码清单-s1ch09b-192页b：在登录逻辑中使用会话作用域的bean
     * @return
     */
    public boolean login() {
        boolean result = false;
        String userName = this.username;
        String passWord = this.password;

        if ("me123".equals(userName) && "ps456".equals(passWord)) {
            result = true;

            // 登录成功之后，将用户名存储在会话作用域 bean 中
            loggedUserManagementService.setUsername(username);
        }

        return result;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
